#include "mainwindow.h"
#include "ui_mainwindow.h"

maps mymap;
//键盘事件控制玩家移动射击
void MainWindow::keyPressEvent(QKeyEvent *event)
{if(!myt->life){return;}
    switch (event->key())
    {
        case 87: myt->moveflag=true;myt->direction=1; break;  //'w'
        case 65: myt->moveflag=true;myt->direction=3; break; //'a'
        case 83: myt->moveflag=true;myt->direction=2; break;   //'s'
        case 68: myt->moveflag=true;myt->direction=4; break; //'d'
        case 74:     //'j'
                 {
myt->shoot();
                 }
                 myt->moveflag=true; break; //发子弹
}
}
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)

    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
        initmap();//qDebug("初始化");
        this->setWindowTitle(QString(" 游戏界面"));

}
void MainWindow::paintEvent(QPaintEvent*event)
{
    QPainter painter(this);
    //画底色
    painter.setPen(Qt::white);//设置画笔颜色
      painter.setBrush(Qt::white);//设置填充颜色
      painter.drawRect(0,0,520,520);
      //画基地
QPixmap HQ(":/:/new/wall/image/item/HQ.png");
painter.drawPixmap(240,480,40,40,HQ);
//画地图//0气,1泥土,2草，3冰，4砖，5铁
for(int i=0;i<maps::mapy;i++)
{
    for(int j=0;j<maps::mapx;j++)
    {
        if(maps::mmap[i][j]==1)
        {QPixmap temp(":/:/new/wall/image/ground/dirt.png");
           painter.drawPixmap(j*20,i*20,20,20,temp );
        }
        if(maps::mmap[i][j]==2)
        {QPixmap temp(":/:/new/wall/image/ground/grass.png");
            painter.drawPixmap(j*20,i*20,20,20,temp );
    }
        if(maps::mmap[i][j]==3)
        {
            QPixmap temp(":/:/new/wall/image/ground/ice.png");
                           painter.drawPixmap(j*20,i*20,20,20,temp );
        }
        if(maps::mmap[i][j]==4)
        {
            QPixmap temp(":/:/new/wall/image/wall/brick.png");
                           painter.drawPixmap(j*20,i*20,20,20,temp );
        }
        if(maps::mmap[i][j]==5)
        {
            QPixmap temp(":/:/new/wall/image/wall/iron.png");
                           painter.drawPixmap(j*20,i*20,20,20,temp );
        }
    }
}
        //画玩家坦克
if(myt->life){
    if(myt->buff3)
    {QPixmap wd(mybuff.wdpic);
     painter.drawPixmap(myt->posx,myt->posy,40,40,wd);

    }
QPixmap mytankpic(myt->pic[myt->direction-1]);
painter.drawPixmap(myt->posx,myt->posy,40,40,mytankpic);
//画玩家子弹
if(myt->mybullet->moving)
{
    QPixmap bul(myt->mybullet->pic);
    painter.drawPixmap(myt->mybullet->posx,myt->mybullet->posy,4,4, bul);
}
}
else
{myt->reborn+=1;
    if(myt->reborn==25){
    myt->life=true;
    maps::playerhp=1;
    myt->posx=300;
    myt->posy=300;myt->reborn=0;}}
//画敌人坦克和子弹
for(int i=0;i<maps::enemynumber;i++)
{if(enemy[i].is_life<=0){continue;}
    if(enemy[i].mybullet->moving)
    {
        QPixmap bul(enemy[i].mybullet->pic);
        painter.drawPixmap(enemy[i].mybullet->posx,enemy[i].mybullet->posy,4,4, bul);
    }
    if(enemy[i].buff3)
    {
        QPixmap wd(mybuff.wdpic);
         painter.drawPixmap(maps::enemyposx[i],maps::enemyposy[i],40,40,wd);
    }
 QPixmap enemytankpic(enemy[i].pic[enemy[i].mode][enemy[i].direction-1]);
 painter.drawPixmap(maps::enemyposx[i],maps::enemyposy[i],40,40,enemytankpic);
}
//画buff
for(int i=0;i<3;i++)
{
    if(mybuff.buffpos[i][0])
    {
         QPixmap bufp(mybuff.buffpic[mybuff.buffpos[i][0]-1]);
          painter.drawPixmap(mybuff.buffpos[i][1]*20,mybuff.buffpos[i][2]*20,20,20,bufp);
    }
}
//显示生命值，得分之类的
QFont font2("Courier",15);
   painter.setFont(font2);
   painter.setPen(Qt::blue);
   painter.setBrush(Qt::blue);
   painter.drawText(600,400,"血量"+QString::number(maps::playerhp));
};
void MainWindow::initmap()
{

    mybuff.initbuff();
    enemy=new enemytank[maps::enemynumber];
    //qDebug()<<maps::enemynumber<<"敌人数量";
    for(int i=0;i<maps::enemynumber;i++)
    {
        enemy[i].initenemy
                (maps::enemyposx[i],
                maps::enemyposy[i],1,1,maps::mode[i]);
        enemy[i].num=i;enemy[i].is_life=1;//qDebug()<<enemy[0].is_life;
    }

    myt =new mytank;
    timer=new QTimer(this);//设定计时器
       timer->start(15);//计时器时间
myt->inittank(300,300,1,1,true);
connect(timer,SIGNAL(timeout()),SLOT(map_update()));
maps::myx=myt->posx;maps::myy=myt->posy;
       is_start=true;

};
void MainWindow::map_update()
{myt->playerupdate();
    //qDebug()<<enemy[0].is_life;
    if(maps::playerhp<=0){myt->life=false;myt->posx=600;myt->posy=600;}
    maps::myx=myt->posx;maps::myy=myt->posy;
    {update();}result();
};
MainWindow::~MainWindow()
{
    delete ui;qDebug("析构");
}
void MainWindow::result()
{if(!maps::start){return;}
    if(maps::HQ==false)
    {emit gameover();maps::start=false;
        timer->stop();
        this->close();//qDebug("游戏结束输了");return;
    }
   // qDebug()<<maps::enemynumber;
    for(int i=0;i<maps::enemynumber;i++)
    {
        if(enemy[i].is_life>0)
        {return;}
    }
    //qDebug()<<enemy[0].is_life;
    emit gameover();maps::start=false;
    timer->stop();
    this->close();//qDebug("游戏结束赢了");
};
void MainWindow::keyReleaseEvent(QKeyEvent *ev)
{
    switch (ev->key())
    {
        case 87: myt->moveflag=false; break;  //'w'
        case 65: myt->moveflag=false; break;  //'a'
        case 83: myt->moveflag=false; break;  //'s'
        case 68: myt->moveflag=false; break;  //'d'
        case 74: myt->moveflag=false; break;//j

    }
}
